package com.example.demo.controller;

import cn.hutool.core.date.DateTime;
import com.example.demo.annotation.Distinct;
import com.example.demo.entity.*;
import com.example.demo.finalUtils.FinalUtils;
import com.example.demo.service.CountService;
import com.example.demo.service.DaKaService;
import com.example.demo.service.DakamingxiService;
import com.example.demo.service.DownloadappcountService;
import com.example.demo.util.LoginAuthUtil;
import com.example.demo.util.RestHttp;
import com.example.demo.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;


/**
 * Controller
 */
@Controller
public class DemoController {
    private static final Logger LOG = LoggerFactory.getLogger(LoginAuthUtil.class);

    @Autowired
    FinalUtils finalUtils;
    @Value("${net.tenantName}")
    private  String tenantName;
    @Value("${net.tenantPwd}")
    private  String tenantPwd;
    @Value("${net.host}")
    private  String host;
    @Value("${net.port}")
    private String port;
    @Autowired
    private DakamingxiService dakamingxiService;
    @Autowired
    RestHttp restHttp;


    @Autowired
    private CountService countService;
    @Autowired
    private DaKaService daKaService;
    @Resource
    private DownloadappcountService downloadappcountService;
    /**
     * 下载
     * @return
     */
    @RequestMapping("/CloudEco/download")
    public String download(){

        return "success";
    }
    /**
     * 下载
     * @return
     */
    @RequestMapping( value = "/CloudEco/downloadCount", method = RequestMethod.GET)
    @ResponseBody
    public String downloadCount(){
        int count=1;
        Downloadappcount downloadappcount=downloadappcountService.query();
        if (downloadappcount==null){
            Downloadappcount downloadappcount1=new Downloadappcount();
            downloadappcount1.setDownloadcount(count);
            downloadappcount1.setCreatetime(new DateTime());
            downloadappcount1.setUpdatetime(new DateTime());
            downloadappcountService.insert(downloadappcount1);
        }else {
            downloadappcount.setDownloadcount(downloadappcount.getDownloadcount()+count);
            downloadappcount.setUpdatetime(new DateTime());
            downloadappcountService.update(downloadappcount);
        }

        return "success";

    }

    /**
     * count
     * @return
     */
    @RequestMapping("/CloudEco/count")
    public String count(){

        return "count";
    }


    /**
     * 测试用 首页
     * @return
     */
    @GetMapping("/CloudEco")
    public String index() {
        return "success1.html";
    }

    /**
     * 查询当日打卡数据
     * @param mac
     * @param SvcID
     * @return
     * @throws KeyManagementException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping(value="/CloudEco/QueryInfo", method = RequestMethod.GET)
    @ResponseBody
    @Distinct
    public Map privilege(@RequestParam String mac,@RequestParam String SvcID) throws KeyManagementException, NoSuchAlgorithmException,
            KeyStoreException, IOException, ParseException {
        LocalDate localDate=LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");//获得当前日期
        Map map=new HashMap();
        if (SvcID!=null&&SvcID.equals("2"))//根据生态库
        if (mac!=null&&!mac.equals(""))
        {
            //查询云管理数据
            //dakamingxiService.queryByAccount(dakayonghu.getAccount(), localDate.format(formatter));
           //Dakayonghu dakayonghu=  dakayonghuService.queryByMac(mac);

//            if (dakayonghu.getPrivilege()==null||dakayonghu.getPrivilege().equals("0"))
//            {
//                //无权限
//                map.put("errcode","1");
//                map.put("errmsg","没有权限查看");
//            }else {
                //查询云管理数据
                //List<Querybyaccountandmac> querybyaccountandmacList=dakamingxiService.queryByAccount(/*dakayonghu.getAccount()*/mac, localDate.format(formatter));
               List<Querybyaccountandmac> querybyaccountandmacList=new ArrayList<>();
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","停用公告!"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","该版本已经停"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","用。需删除。"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","请下载新版本"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","应用:"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","http://114"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00",".115.173.102:"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","8080/daka"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","/CloudEco"));
               querybyaccountandmacList.add(new Querybyaccountandmac("2020-00-00 00:00:00","/download"));

//                if (dakayonghu.getPrivilege().equals("1"))
//                {
//                    //权限为 1 查询自己
//                    querybyaccountandmacList=dakamingxiService.queryByAndMacDate(mac ,localDate.format(formatter));
//                }else if (dakayonghu.getPrivilege().equals("2"))
//                {
//                    //权限为 2 查询所有
//                    querybyaccountandmacList=dakamingxiService.queryByDate(localDate.format(formatter));
//                }
                //对应的mac没有账号的，其uname=mac的值
                //Querybyaccountandmac querybyaccountandmac=new Querybyaccountandmac();
                //querybyaccountandmac.setFirsttime("00:00:00");
               // querybyaccountandmac.setUname("该版本已更新，请到 114.115.173.102:8080/daka/CloudEco/download 下载新应用。");
//                querybyaccountandmacList.stream().forEach(querybyaccountandmac -> {
//                    querybyaccountandmac.setUname("该版本已更新，请到114.115.173.102:8080/daka/CloudEco/download下载新应用。");
//                    querybyaccountandmac.setFirsttime("00:00:00");
//                });
               // querybyaccountandmacList.add(querybyaccountandmac);
//                querybyaccountandmacList.stream().sorted(Comparator.comparing(Querybyaccountandmac::getFirsttime).reversed()).collect(Collectors.toList());
                map.put("errcode","0");
                map.put("errmsg","");
                map.put("data",querybyaccountandmacList);
                map.put("SvcID",SvcID);

            //}

        }
        return   map;

    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/CloudEco/count2")
    public String  count2(Model model){
        DakaCount dakaCount= countService.count();
        System.out.println(dakaCount.getCjsj());
        model.addAttribute("dakaCount",dakaCount);
        DakaCount dakaCountList=countService.dakaCount();
        model.addAttribute("dakaCountList",dakaCountList);
        return "count";
    }
    /**
     *
     * @param model
     * @return
     */
    @RequestMapping("/CloudEco/dedmo")
    @ResponseBody
    public String  dedmo(Model model){
        List<Dakamingxi> dakamingxiList= dakamingxiService.queryAll();
        List<Dakamingxi> dakamingxiList1= new ArrayList<>();
        for (Dakamingxi dakamingxi:dakamingxiList){
            String mac=dakamingxi.getMac().trim().replace(":","-").toLowerCase();
            dakamingxi.setMac(mac);

            dakamingxiList1.add(dakamingxi);
        }
        dakamingxiService.updateall(dakamingxiList1);

        return "count";
    }
    /**
     * 测试用
     */

    @RequestMapping("/CloudEco/test")
    @ResponseBody
    @Distinct
    public Map testx(@RequestParam String mac,@RequestParam String SvcID) throws KeyManagementException, NoSuchAlgorithmException,
            KeyStoreException, IOException, ParseException {
        Map map=new HashMap();
        List list=daKaService.getLogList();
        map.put("data",list);
        return   map;

    }
}
